import {
  uploadImg,
  updatePhone,
  updateName,
  updateGender
} from '../../util/api';
//导入验证js
import WxValidate from "../../util/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    portrait: [], //头像
    phone: 0, //手机号码
    username: "", //昵称
    gender: "", //性别
    showPhoneDialog: true, //手机号码对话框显示
    showNameDialog: true //昵称对话框显示
  },

  //点击头像
  clikePortrait() {
    this.showModal()
  },
  //显示头像选择框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  //更换头像
  replaceImg() {
    var _this = this;
  
    wx.chooseImage({
      count:1,
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        _this.setData({
          portrait: res.tempFilePaths, //返回的 res.tempFilePaths 是数组
        })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFiles[0].path;
        
        wx.uploadFile({
          url: 'https://api.zhouyfeng.cn/?s=Examples_Upload.go',
          filePath: tempFilePaths,
          header: { 'Content-Type': 'application/x-www-form-urlencoded' },
          method: 'post',
          name: 'file',
           formData: { 
              'name': 'name',
            'post_date':'post_date'
              },
          success: function (res) {
            
            var user = wx.getStorageSync('userInfo');
            var data = JSON.parse(res.data)
            var formData = {
              avatarUrl: JSON.stringify(data.data.url),
              open_id: JSON.stringify(user.open_id)
            }
            uploadImg(formData).then((res) => {
         
              var pages = getCurrentPages(); //当前页面栈
              //返回前一个页面时，刷新前一页面数据
              if (pages.length > 1) {
                var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
                beforePage.changeData(); //触发父页面中的方法
              };
              _this.hideModal()
            })
          }
        })
      },
       
   
    })
  },
  //使用微信头像
  useImg() {
    var _this = this;
    wx.getUserInfo({
      success: function (res) {
        var user = wx.getStorageSync('userInfo');
        var formData = {
          avatarUrl: JSON.stringify(res.userInfo.avatarUrl),
          open_id: JSON.stringify(user.open_id)
        }
        _this.setData({
          portrait: res.userInfo.avatarUrl, //返回的 res.tempFilePaths 是数组
        })
        uploadImg(formData).then((res) => {
          var pages = getCurrentPages(); //当前页面栈
          //返回前一个页面时，刷新前一页面数据
          if (pages.length > 1) {
            var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
            beforePage.changeData(); //触发父页面中的方法
          };
          _this.hideModal()
        })
      }
    })
  },

  //弹出手机号码对话框
  showPhone() {
    setTimeout(function () {
      this.setData({
        showPhoneDialog: false
      })
    }.bind(this), 200)
  },


  //关闭手机号码对话框
  cancelPhoneDialog() {
    this.setData({
      showPhoneDialog: true
    })
  },
  //弹出昵称对话框
  showName() {
    setTimeout(function () {
      this.setData({
        showNameDialog: false
      })
    }.bind(this), 200)
  },
  //关闭昵称对话框
  cancelNameDialog() {
    this.setData({
      showNameDialog: true
    })
  },
  //手机号码确认键
  phoneBox: function (e) {
    let that = this;
    var user = wx.getStorageSync('userInfo');
    let phone = WxValidate('phone', e.detail.value.phone); //验证电话号码
    var formData = {
      phone: JSON.stringify(e.detail.value.phone),
      open_id: JSON.stringify(user.open_id)
    }
    if (phone) {
      updatePhone(formData).then((res) => {
        if (res.data.ret == 200) {
         
          that.setData({
            phone: e.detail.value.phone
          });
          wx.setStorage({
            data: e.detail.value.phone,
            key: 'phone',
          });

          that.cancelPhoneDialog()
        }
      })
    }
  },
  //昵称确认键
  nameBox: function (e) {
    let that = this;
    var user = wx.getStorageSync('userInfo');
    var formData = {
      username: JSON.stringify(e.detail.value.name),
      open_id: JSON.stringify(user.open_id)
    }
    updateName(formData).then((res) => {
      if (res.data.ret == 200) {
   
        that.setData({
          username: e.detail.value.name
        });
        wx.setStorage({
          data: e.detail.value.name,
          key: 'username',
        });
        var pages = getCurrentPages(); //当前页面栈
        //返回前一个页面时，刷新前一页面数据
        if (pages.length > 1) {
          var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
          beforePage.changeData(); //触发父页面中的方法
        };
        that.cancelNameDialog()
      }
    })
  },
  //点击性别
  clikeGender() {
    this.showGender()
  },
  //显示性别选择框
  showGender: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      genderBox: animation.export(),
      showModalGender: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        genderBox: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideGender: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      genderBox: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        genderBox: animation.export(),
        showModalGender: false
      })
    }.bind(this), 200)
  },
  //选择性别
  genderInfo(e) {
    var that = this;
    var gender = e.currentTarget.dataset.gender;
    var user = wx.getStorageSync('userInfo');
    var formData = {
      gender: gender,
      open_id: JSON.stringify(user.open_id)
    };
    updateGender(formData).then((res) => {

      that.setData({
        gender: gender,
      });
      wx.setStorage({
        data: gender,
        key: 'gender',
      });
      that.hideGender()
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var username = wx.getStorageSync('username');
    var gender = wx.getStorageSync('gender');
    var portrait = wx.getStorageSync('portrait');
    var phone = wx.getStorageSync('phone');


    this.setData({
      portrait: portrait,
      gender: gender,
      username: username,
      phone: phone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})